# -*- codeing = utf-8 -*-

import jieba  #分词
import matplotlib #绘图，数据可视化
import wordcloud #词云
from PIL import Image #图片处理
import numpy as np #矩阵运算
import sqlite3

from matplotlib import pyplot as plt

conn = sqlite3.connect('movie.db')
c = conn.cursor()
sql = 'select instroduction from movie'
data = c.execute(sql)
text = ""
for row in data:
    text += row[0]
c.close()
conn.close()

#分词
cut = jieba.cut(text)
string = " ".join(cut)
print(len(string))

#设置遮罩图片
img = Image.open(r'static\img\tree2.jpg')
img_array = np.array(img) #将图片转为数组
wc = wordcloud.WordCloud(
    background_color='white',
    mask=img_array,
    width=1800,
    height=1800,
    font_path="msyh.ttc" #字体
).generate_from_text(string)

#绘制图片
# fig = plt.figure(1)
plt.imshow(wc)
plt.axis("off") #是否显示坐标轴
#plt.show() #显示生成图片

#输出词云图片
plt.savefig(r'static\img\wordcloud.png',dpi=600)